#!/bin/bash

#
# libprogressbar -- by Dario Berzano <dario.berzano@gmail.com>
#
# Part of the lfscripts suite -- http://lfscripts.googecode.com/
#
# A library for a progress bar for shell scripts
#

#
# Global variables
#

# Progressbar related
export PROGSYM='|\\-/'
export PROGSTAT
export PROGLEN

#
# Functions
#

# Initializes the progress indicator
function ProgressStart() {
  local MSG
  PROGLEN=${#PROGSYM}
  PROGSTAT=0
  if [ "$1" != "" ]; then
    MSG=$1
  else
    MSG="Executing..."
  fi

  if [ "$VERBOSE" == 1 ]; then
    echo "$MSG"
  else
    echo -n "$MSG"' '
  fi
}

# Stops the progress indicator
function ProgressStop() {
  local MSG
  if [ "$1" != "" ]; then
    MSG=$1
  else
    MSG="done"
  fi

  if [ "$VERBOSE" == 1 ]; then
    echo "$MSG"
  else
    echo -e '\b'"$MSG"
  fi
}

# Increments the progress indicator
function ProgressStep() {
  if [ "$VERBOSE" == 1 ]; then
    return
  fi
  echo -en '\b'${PROGSYM:PROGSTAT:1}
  let PROGSTAT++
  if [ $PROGSTAT == $PROGLEN ]; then
    PROGSTAT=0
  fi
}
